package com.cxq.boot.controller;

import com.cxq.boot.dto.NotificationDTO;
import com.cxq.boot.enums.NotificationTypeEnum;
import com.cxq.boot.exception.CustomizeException;
import com.cxq.boot.model.User;
import com.cxq.boot.service.NotificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;

@Controller
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    @GetMapping("/notification/{id}")
    public String notify(HttpServletRequest request,
                         @PathVariable("id") Long id) {
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/";
        }
        //获取通知信息
        NotificationDTO notificationDTO = notificationService.read(id, user);

        if (NotificationTypeEnum.REPLY_COMMENT.getType() == notificationDTO.getType() ||
                NotificationTypeEnum.REPLY_QUESTION.getType() == notificationDTO.getType())
            return "redirect:/question/" + notificationDTO.getOuterId();
        return "profile";//redirect:/
    }
}
